<?
session_start();

include_once("../classCol.php");
include_once("../helpers/CHtml.php");
include_once("../helpers/CJavascript.php");
include_once("../models/GFetchers.php");
include_once("../models/GHandlers.php");
include_once("../models/GlobalHandlers.php");
include_once("../models/GlobalFetchers.php");
include_once("../helpers/Composer.php");
include_once("../helpers/ComposerSourceDb.php");
include_once("../controllers/GlobalProcessors.php");

include_once("controllers/local.php");
include_once("models/fetchers.php");
include_once("models/handlers.php");

$thisMdl    = "CASH-BANK";
$mdlNumber  = "31";

$Misc        = new Misc;
$GHtml       = new CGHtml;
$GJs         = new CGJavascript;
$CLFetchers  = new CLFetchers;
$CLHandlers  = new CLHandlers;
$CLocal      = new CLocal;
$GProcessors = new GlobalProcessors;

$CFetchers   = new CGFetchers;
$CHandlers   = new CGHandlers;

$SourceDb   = new Composer;
$SourceDb   = $SourceDb->init("db");

$SourceDb->add("srcDbHdr",                  "C_B_HDR");
$SourceDb->add("srcDbDtl",                  "C_B_DTL");
$SourceDb->add("srcDbSubDtl",               "C_B_SUB_DTL");
$SourceDb->add("srcDbMutCbDtl",             "MUT_C_B_DTL");
$SourceDb->add("srcDbMstCb",                "MST_CASH_BANK");
$SourceDb->add("srcDbCbTx",                 "MST_CASH_BANK_TX");
$SourceDb->add("srcDbMstCurrency",          "MST_CURRENCY");
$SourceDb->add("srcDbTxType",               "MST_CASH_BANK_TX");
$SourceDb->add("srcDbCustomer",             "CUSTOMER");
$SourceDb->add("srcDbSupplier",             "SUPPLIER");
$SourceDb->add("srcDbMstGlAcc",             "MST_GL_ACC");
$SourceDb->add("srcDbMstGlSubAcc",          "MST_GL_SUB_ACC");
$SourceDb->add("srcDbMstGlOptAcc",          "MST_GL_SUB_ACC_OPT");
$SourceDb->add("srcDbTmpDtlIvcSupplier",    "C_B_IVC_SUPP_DTL_TMP");
$SourceDb->add("srcDbTmpSubIvcSupplier",    "C_B_IVC_SUPP_SUB_DTL_TMP");
$SourceDb->add("srcDbMutIvcSupplier",       "MUT_IVC_SUPPLIER");
$SourceDb->add("srcDbDtlIvcSupplier",       "C_B_IVC_SUPP_DTL");
$SourceDb->add("srcDbSubIvcSupplier",       "C_B_IVC_SUPP_SUB_DTL");
$SourceDb->add("srcDbMutDpSupplier",        "MUT_DP_SUPPLIER");
$SourceDb->add("srcDbMutDp",                "MUT_DP_SUPPLIER");
$SourceDb->add("srcDbDtlCclDpSupplier",     "C_B_CCL_DP_SUPP_DTL");
$SourceDb->add("srcDbSubCclDpSupplier",     "C_B_CCL_DP_SUPP_SUB_DTL");
$SourceDb->add("srcDbTmpDtlCclDpSupplier",  "C_B_CCL_DP_SUPP_DTL_TMP");
$SourceDb->add("srcDbTmpSubCclDpSupplier",  "C_B_CCL_DP_SUPP_SUB_DTL_TMP");


$srcDb = "USERID_DTL";

//check if the object of entry is exist if doesnt then create one
//First opening
if( !$_POST["entry"]    &&
    !$_POST["addrow"]   &&
    !$_POST["confirm"]  &&
    !$_POST["back"]     &&
    !$_POST["droprow"]  &&
    !$_POST["view"]     &&
    !$_POST["cat_pick"] ){

  /**
  * Reset the modify_mode
  *
  */
  $_SESSION["modify_mode"] = "N";

  $_SESSION["tmpJid"]      = "";

	/**
	* We dont have anything to Pick
	* so we are by pass the category pick
	*
	*/
	//require("entry_category_pick.php");

	require("entry_data_body.php");
}

/**
* if supplier picked then continue
*
*/
if($_POST["cat_pick"]){

	require("entry_data_body.php");
}

//if entry or confirm or back selected
if( $_POST["entry"]   ||
    $_POST["confirm"] ||
    $_POST["back"]    ){

	/**
	* check if the one has priviledge to entry
	*
	*/
	$UID = unserialize($_SESSION["serUID"]);

	if($UID->getPrivsByNumber( $mdlNumber, "ENTRY_RIGHTS") > 0){

		require("entry_details_check.php");
	}else{

		$Misc->usrError("Authed Error reason :".$chk_res." ");
	}
}

if($_POST["addrow"]){

	require("entry_add_row.php");
}

/**
* Presquities to use this item are:
*
* 1. to use dropBoxEntp[] auto numbering index
*    don't do it manually
* 2. The value of the dropBox should
*    take normal numbering instead of array numbering
*
*/
if($_POST["droprow"]){

	//var_dump($_POST["dropBox"]);

	/**
  * splice the dummy
  * The first 2 params are to shrink the array
  * e.g: array_splice($dummy, 2); is to shrink dummy to 2 elements
  *
  * The last 2 params is to concanate the array
  * it tells how many array to concat and the content of the array
  *
  * dropPoint needs to be reduce by 1 so we could have the cursor
  * of array for $dummy at the correct posisition
  * e.g: dropPoint = 3, meaning the 3rd elements is going to be dust off
  *      so we only save the first and 2nd element. Then use splice
  *      to concat the 4th...nElement with the first 2 element.
  *
  */

	/**
	* get how many selected drops
	*
	*/
	$dropTimes = sizeof($_POST["dropBox"]);

	//usrDebug("dropTimes",$dropTimes);

	/**
	* Reupdate me please since
	* This code already built by MenuAccelerator
	*
	*/
	//list all records name
	$lstArray = array("glAccEnt",
	                  "glAccCodeEnt",
	                  "glSubAccEnt",
	                  "glSubAccCodeEnt",
                  	"descpEnt",
                  	"amountEnt",
                  	"netEnt");


	//shift it for many times
	foreach($lstArray as $list){

		for($i = $dropTimes ; $i > 0 ; $i--){

			/**
			* Since $_POST["dropBox"] index is using array numbering
			* then we need to reduce the droppoint by 1.
			* E.g: $dropTimes = 3 => $_POST["dropBox"][3] will result Error
			*      since the dropTimes is trying to point to 3rd element
			*      of $_POST["dropBox"], which array wise is at [2] meaning 3rd element
			*
			*/
			$dropPoint = $_POST["dropBox"][($i-1)];

			/**
			* slice it! so we could have the buffer's tail
			*
			*/
			$bufTail   = array_slice($_POST[$list], $dropPoint);

			//var_dump($bufTail);

			//replace it
			array_splice($_POST[$list],               /* The original array or dummy */
			            ($dropPoint - ARRAY_NUMBER),  /* Calculate the cursor, since it comes with array numbering */
			            sizeof($bufTail),             /* The sizeof array concat with */
			            $bufTail);                    /* The array we concat with */

			//var_dump($_POST[$list]);
		}
	}
	unset($list);

	$new_init_num = $_SESSION["init_num"] - $dropTimes;
	//echo	"<br/>\ :".$new_init_num ;
	if($new_init_num >= 0)

		$_SESSION["init_num"] = $new_init_num;
	else

		$_SESSION["init_num"] = 0;

	require("entry_restore_me.php");
}

echo "<br/><br/><br/><br/>";
?>